﻿using System;
using System.Collections.Generic;
using System.Text;
using ExpressionUtil.Node.Common;
using ExpressionUtil.Common;
using System.Diagnostics;
using ExpressionUtil.Exceptions;
using System.Runtime.Serialization;

namespace ExpressionUtil.Node
{
    /// <summary>
    /// 变量节点
    /// </summary>
    [Serializable]
    internal class VariableNode : Common.Node, ISerializable
    {
        /// <summary>
        /// 变量名称
        /// </summary>
        private string variableName;

        /// <summary>
        /// 当前类型
        /// </summary>
        private ExecuteRunTimeType currentType;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">变量名称</param>
        public VariableNode(string name)
            : base()
        {
            this.VariableName = name;
            this.currentType = ExecuteRunTimeType.None;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="info">要填充数据的 System.Runtime.Serialization.SerializationInfo</param>
        /// <param name="context">此序列化的目标</param>
        public VariableNode(SerializationInfo info, StreamingContext context)
            : base()
        {
            this.variableName = info.GetString("variableName");
            this.currentType = (ExecuteRunTimeType)info.GetValue("currentType", typeof(ExecuteRunTimeType));
        }

        /// <summary>
        /// 获取或设置变量名称
        /// </summary>
        public string VariableName
        {
            get
            {
                return this.variableName;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new InvalidOperationException("Variable name must be not null or empty.");
                }
                this.variableName = value;
            }
        }

        public ExecuteRunTimeType CurrentType
        {
            get
            {
                return this.currentType;
            }
            set
            {
                this.currentType = value;
            }
        }

        /// <summary>
        /// 执行节点运算
        /// </summary>
        /// <param name="context">执行参数</param>
        /// <returns>执行结果的类型</returns>
        public override ExecuteRunTimeType Execute(ExecuteContext context)
        {
            if (this.currentType == ExecuteRunTimeType.Decimal)
            {
                context.GlobalTempNum = context.DecimalVariables[this.variableName];
            }
            else if (this.currentType == ExecuteRunTimeType.Bool)
            {
                context.GlobalTempBool = context.BoolVariables[this.variableName];
            }
            else if (this.currentType == ExecuteRunTimeType.None)
            {
                throw new RunTimeTypeErrorException("Variable value must be setted before execute.");
            }
            return this.currentType;
        }

#if DEBUG
        protected override void DumpCore()
        {
            Debug.WriteLine("VariableNode,name:" + this.variableName + ",type:" + this.currentType.ToString());
        }
#endif

        #region ISerializable 成员
        /// <summary>
        /// 获取对象数据
        /// </summary>
        /// <param name="info">要填充数据的 System.Runtime.Serialization.SerializationInfo</param>
        /// <param name="context">此序列化的目标</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("variableName", this.variableName);
            info.AddValue("currentType", this.currentType, typeof(ExecuteRunTimeType));
        }

        #endregion
    }
}
